import { IMyObject, MyObject } from "./MyObject";
import { ysReadline, ysReadlineClose } from "../read";
import {
  AddClassMenuItem,
  AddStudentsMenuItem,
  IMenuItem,
  ShowClassedGradeMenuItem,
} from "./IMenu";

export interface IControler extends IMyObject {
  run(): any;
}

export class Controler extends MyObject implements IControler {
  count = 0;

  menus: (IMenuItem | IControler)[] = [
    new AddClassMenuItem(),
    new AddStudentsMenuItem(),
    new ShowClassedGradeMenuItem(),
  ];

  async run() {
    while (true) {
      this.menus.forEach((item, index) => {
        console.log(`${index}:${item.toString()}`);
      });

      try {
        const menuIndex = await ysReadline("请选择：");
        await this.menus[Number(menuIndex)].run();
      } catch (e) {
        console.log(e);
        ysReadlineClose();
        return;
      }
    }
  }
}
